<template>
  <div class="p-2 page-flex">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="项目名称" prop="proName">
              <el-input v-model="queryParams.proName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="审核状态" prop="auditStatus">
              <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" style="width: 240px" clearable>
                <el-option v-for="item in approval_commit_status" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item style="margin-left: auto">
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <div style="display: flex; align-items: center; gap: 8px">
              <el-select v-model="sortField" placeholder="请选择排序字段" style="width: 170px" clearable
                @change="handleSortChange">
                <el-option label="项目名称" value="proId" />
                <el-option label="周序列" value="weekOrder" />
                <el-option label="审核状态" value="auditStatus" />
              </el-select>
              <el-button v-if="sortField" :icon="sortOrder === 'asc' ? 'ArrowUp' : 'ArrowDown'" @click="toggleSortOrder"
                type="info" plain size="small" :title="sortOrder === 'asc' ? '当前为正序，点击切换为逆序' : '当前为逆序，点击切换为正序'">
                {{ sortOrder === 'asc' ? '正序' : '逆序' }}
              </el-button>
            </div>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="weeklogList" row-key="id" border>
        <!-- 序号 -->
        <el-table-column label="序号" align="center" width="80">
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <!-- 项目名称 -->
        <el-table-column label="项目名称" align="center" prop="proName" show-overflow-tooltip />

        <!-- 项目编号 -->
        <el-table-column label="项目编号" align="center" prop="proNumber" show-overflow-tooltip />

        <!-- 第几周 -->
        <el-table-column label="第几周" align="center" prop="weekOrder" width="100">
          <template #default="scope">
            <el-tag type="primary">第{{ scope.row.weekOrder }}周</el-tag>
          </template>
        </el-table-column>

        <!-- 审核状态 -->
        <el-table-column label="审核状态" align="center" prop="auditStatus" width="120">
          <template #default="scope">
            <dict-tag :options="approval_commit_status" :value="scope.row.auditStatus" />
          </template>
        </el-table-column>

        <!-- 操作 -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
          <template #default="scope">
            <el-button link type="primary" v-hasPermi="['project:weeklog:audit']" icon="View"
              @click="handleAudit(scope.row)">审核</el-button>
          </template>
        </el-table-column>

        <template #empty>
          <el-empty description="暂无周总结数据" />
        </template>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ProWeeklogAudit" lang="ts">
import { listProWeeklogAudit } from '@/api/project/log/weeklog';
import { useRouter } from 'vue-router';
import type { FormInstance } from 'element-plus';
import type { ProWeeklogVO } from '@/api/project/log/weeklog/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();
const { approval_commit_status } = toRefs<any>(proxy?.useDict('approval_commit_status'));

const weeklogList = ref<ProWeeklogVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

// 排序相关
const sortField = ref<string | undefined>(undefined);
const sortOrder = ref<string>('asc'); // 设置默认为正序

const queryFormRef = ref<FormInstance>();

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    proName: undefined,
    auditStatus: undefined,
    params: {},
    orderByColumn: undefined,
    isAsc: undefined
  }
});

const { queryParams } = toRefs(data);

/** 查询周总结审核列表 */
const getList = async () => {
  loading.value = true;
  try {
    const params = { ...queryParams.value };
    // 处理排序参数
    if (sortField.value && sortOrder.value) {
      params.orderByColumn = sortField.value;
      params.isAsc = sortOrder.value;
    }
    const res = await listProWeeklogAudit(params);
    weeklogList.value = res.rows;
    total.value = res.total;
  } catch (error) {
    console.error('获取列表失败:', error);
    proxy?.$modal.msgError('获取列表失败');
  } finally {
    loading.value = false;
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  // 重置排序条件
  sortField.value = undefined;
  sortOrder.value = 'asc';
  queryFormRef.value?.resetFields();
  queryParams.value.params = {};
  handleQuery();
};

/** 审核按钮操作 */
const handleAudit = (row: ProWeeklogVO) => {
  // 跳转到审核详情页面
  router.push(`/project/weeklog-audit/index/${row.id}`);
};

// 处理排序变化
const handleSortChange = () => {
  // 如果选择了排序字段但没有选择排序顺序，则设置默认值
  if (sortField.value && !sortOrder.value) {
    sortOrder.value = 'asc';
  }
  handleQuery();
};

// 切换排序顺序
const toggleSortOrder = () => {
  sortOrder.value = sortOrder.value === 'asc' ? 'desc' : 'asc';
  handleQuery();
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
/* 页面弹性布局 */
.page-flex {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 16px);
}

.el-table {
  margin-bottom: 16px;
}
</style>
